import 'package:flutter/widgets.dart';
import 'package:library_base/entities/stock_out_order_detail_child_entity.dart';
import 'package:library_base/entities/stock_out_order_goods_detail_entity.dart';
import 'package:library_base/ext/double_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/net/apis.dart';
import 'package:library_base/utils/num_util.dart';
import 'package:library_base/utils/toast_util.dart';
import 'package:module_wms/page/outbound_orders/as_order/order_detail/inventory_distribution/base_inventory_distribution_single_controller.dart';
import 'package:get/get.dart';

class InventoryDistributionNoSingleController extends BaseInventoryDistributionSingleController {

  late StockOutOrderDetailChildEntity stockOutOrderDetailChildEntity;

  late Function(StockOutOrderDetailChildEntity stockOutOrderDetailChildEntity, int position) updateUi;

  List<TextEditingController> distributeControllers = [];
  List<FocusNode> distributeFocusNodes = [];

  List<TextEditingController> weightControllers = [];
  List<FocusNode> weightFocusNodes = [];

  late int position;

  @override
  void onCreate() {
    super.onCreate();
    stockOutOrderDetailChildEntity = getArguments()['temporaryData'];
    updateUi = getArguments()['callback'];
    position = getArguments()['index'];
    goodsName = getArguments()['goodsName'];
  }

  @override
  void distribute(){
    if(checkedStockOutOrderGoodsDetailEntity.isEmpty){
      ToastUtil.normal(MultiLanguage.pleaseSelectItemsInStock.tr);
      return;
    }
    for(int i = 0;i<checkedStockOutOrderGoodsDetailEntity.length;i++){
      StockOutOrderGoodsDetailEntity stockOutOrderGoodsDetailEntity = checkedStockOutOrderGoodsDetailEntity[i];
      int index = goodsDetails.indexWhere((element) => stockOutOrderGoodsDetailEntity == element);
      if(distributeControllers[index].text.isEmpty){
        ToastUtil.normal('选中商品数量不能为空');
        return;
      }else{
        if(NumUtil.isNumber(distributeControllers[index].text)){
          if(double.parse(distributeControllers[index].text)<=0.0){
            ToastUtil.normal('选中商品数量至少为1');
            return;
          }
          if(double.parse(distributeControllers[index].text)>stockOutOrderGoodsDetailEntity.usableCount){
            ToastUtil.normal('商品$goodsName数量不能大于当前库存数量${stockOutOrderGoodsDetailEntity.usableCount}');
            return;
          }
        }else{
          ToastUtil.normal('选中商品数量请输入数字');
          return;
        }
      }
      if(weightControllers[index].text.isEmpty){
        ToastUtil.normal('选中商品重量不能为空');
        return;
      }else{
        if(NumUtil.isNumber(weightControllers[index].text)){
          if(double.parse(weightControllers[index].text)<=0.0){
            ToastUtil.normal('选中商品重量至少为1');
            return;
          }
          if(double.parse(distributeControllers[index].text).numAsUnitSubmit()>stockOutOrderGoodsDetailEntity.usableCount){
            ToastUtil.normal('商品$goodsName重量不能大于当前库存重量${stockOutOrderGoodsDetailEntity.usableCount}');
            return;
          }
        }else{
          ToastUtil.normal('选中商品重量请输入数字');
          return;
        }
      }
    }

    var map = {};
    map['stockOutId'] = stockOutId;
    map['goodsCode'] = goodsCode;

    var goodsDetailsList = [];
    double actualOutCount = 0.0;
    double actualOutWeight = 0.0;
    for(int i = 0;i<checkedStockOutOrderGoodsDetailEntity.length;i++){
      var selectData = checkedStockOutOrderGoodsDetailEntity[i];
      int index = goodsDetails.indexWhere((element) => selectData == element);
      var detail = {};
      detail['batchNo'] = selectData.batchNo;
      detail['locationId'] = selectData.locationId;
      detail['stockOutCount'] = double.parse(distributeControllers[index].text);
      detail['stockOutWeight'] = double.parse(weightControllers[index].text).numAsUnitSubmit();

      selectData.stockOutCount = double.parse(distributeControllers[index].text);
      selectData.stockOutWeight = double.parse(weightControllers[index].text).numAsUnitSubmit();

      goodsDetailsList.add(detail);
      actualOutCount = NumUtil.add(actualOutCount, selectData.stockOutCount??0.0);
      actualOutWeight = NumUtil.add(actualOutWeight, selectData.stockOutWeight??0.0);
    }
    map['goodsDetails'] = goodsDetailsList;

    requestUniversal<bool>(Apis.STOCK_OUT_LOCK_GOODS,
        tipSuccess: MultiLanguage.successfulDistributionGoods.tr,
        isShowToastTip: true,
        params: map, resultSuccessCall: (result) async {
          stockOutOrderDetailChildEntity.goodsDetails = checkedStockOutOrderGoodsDetailEntity;
          stockOutOrderDetailChildEntity.actualOutCount = actualOutCount;
          stockOutOrderDetailChildEntity.actualOutWeight = actualOutWeight;
          updateUi(stockOutOrderDetailChildEntity,position);
          Get.back();
        });
  }

  void weightChange(String value,int position){
    if(NumUtil.isNumber(weightControllers[position].text)){

      var stockOutWeight = double.parse(weightControllers[position].text);

      stockOutWeight = double.parse(weightControllers[position].text);
      distributeControllers[position].text = stockOutWeight.numAsUnitSubmit().toString();
    }else{
      distributeControllers[position].text = "";
    }
  }

}
